/*
**  File: AquaFuncts_xplor.c
**
**  This file is part of the AQUA program suite, which is being
**  developed in the NMR Spectroscopy Department, Bijvoet Center for 
**  Biomolecular Research, Utrecht. 
**
**
**  AUTHOR(S) : Ton Rullmann
**  VERSION   : 1.1
**  DATE      : Feb 17, 1997
**
**  This file contains routines for handling Xplor files.
**
**  Most header definitions are present in AquaFuncts_io.c
*/

/****************  function prototypes  ****************/

#include "AquaFuncts_xplor.h"

/* BEGIN_EXTH */

/****************  WriteRestraints_xplor  ****************/
/*
**  Write restraints in Xplor restraint format. 
**  Xplor format:
**
**  assign (resid    2 and  name HA   )(resid   18 and  name HN   ) 4.0 2.2 1.0
** 
**  The restraints are represented by square-well potentials with the upper (u)
**  and lower (l) limits given  by u=i+k and l=i-j where the numbers are
**  entered in the order i,j,k. [Clore et al. (1986) EMBO J. 5, 2729-2735]
*/

void WriteRestraints_xplor( FILE *fp, DRestraint *restrs, int DR_count, 
                            int skip_count, Card ident)
{
    char    *fct_name = "WriteRestraints_xplor";
    size_t  l;
    float   ii, jj, kk;

    fprintf( fp, "!%s\n\n", ident );
    while ( DR_count-- )
    {
	if ( !restrs->keep ) 
	{
	    restrs++;
	    continue;
	}

	fprintf( fp, "assign (" );
	if ( ( l = LenChars( restrs->atom_A.chain, size_ID ) ) )
	{
	    fprintf( fp, "segid " );
	    FPrintChars( fp, restrs->atom_A.chain, l );
	    fprintf( fp, " and " );
	}
	fprintf( fp, "resid %3i", restrs->atom_A.res_num_orig );
	if ( restrs->atom_A.res_insert != ' ' )
	    fprintf( fp, "%c", restrs->atom_A.res_insert );
	fprintf( fp, " and name " );
	FPRINTID( fp, restrs->atom_A.atom_ext );

	fprintf( fp, ") (" );
	if ( ( l = LenChars( restrs->atom_B.chain, size_ID ) ) )
	{
	    fprintf( fp, "segid " );
	    FPrintChars( fp, restrs->atom_B.chain, l );
	    fprintf( fp, " and " );
	}
	fprintf( fp, "resid %3i", restrs->atom_B.res_num_orig );
	if ( restrs->atom_B.res_insert != ' ' )
	    fprintf( fp, "%c", restrs->atom_B.res_insert );
	fprintf( fp, " and name " );
	FPRINTID( fp, restrs->atom_B.atom_ext );

	switch( restrs->stat )
	{
	    case bnd_upper:
		ii = 0.0F;
		jj = 0.0F;
		kk = restrs->up_bnd;
		break;
	    case bnd_lower:
		ii = restrs->lo_bnd;
		jj = 0.0F;
		kk = 9999.0F;
		break;
	    case bnd_uplo:
		ii = restrs->lo_bnd;
		jj = 0.0F;
		kk = restrs->up_bnd - ii;
		break;
	    case bnd_uld:
		ii = restrs->dist;
		jj = ii - restrs->lo_bnd;
		kk = restrs->up_bnd - ii;
		break;
	    default:
		FATAL( "wrong data type" );
                exit(99); /* Will never be executed but for compiler warning */
	}
	
	fprintf( fp, ") %8.3f %8.3f %8.3f\n", ii, jj, kk );
	
	restrs++;
    }
}
